<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-<%= LIQUIBASE_DTD_VERSION %>.xsd
                        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <% const isAutoIncrementDB = reactive || ['mysql', 'mariadb'].includes(prodDatabaseType);
       const isSequenceDB = !reactive && ['postgresql', 'oracle', 'mssql'].includes(prodDatabaseType);
    _%>
    <%_ if (isAutoIncrementDB) { _%>
    <property name="autoIncrement" value="true"/>
    <%_ } _%>
    <%_ if (isSequenceDB) { _%>
    <changeSet id="00000000000000" author="jhipster">
        <createSequence sequenceName="sequence_generator" startValue="1050" incrementBy="50"/>
    </changeSet>
    <%_ } _%>

    <!--
        JHipster core tables.
        The initial schema has the '00000000000001' id, so that it is over-written if we re-generate it.
    -->
    <changeSet id="00000000000001" author="jhipster">
<%_ if (!skipUserManagement || authenticationType === 'oauth2') { _%>
        <createTable tableName="<%= jhiTablePrefix %>_user">
            <%_ if (authenticationType === 'oauth2') { _%>
            <column name="id" type="varchar(100)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <%_ } else { _%>
            <column name="id" type="bigint"<% if (isAutoIncrementDB) { %> autoIncrement="${autoIncrement}"<% } %><% if (reactive) { %> startWith="1050"<% } %> >
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <%_ } _%>
            <column name="login" type="varchar(50)">
                <constraints unique="true" nullable="false" uniqueConstraintName="ux_user_login"/>
            </column>
            <%_ if (authenticationType !== 'oauth2') { _%>
            <column name="password_hash" type="varchar(60)"/>
            <%_ } _%>
            <column name="first_name" type="varchar(50)"/>
            <column name="last_name" type="varchar(50)"/>
            <column name="email" type="varchar(191)">
                <constraints unique="true" nullable="true" uniqueConstraintName="ux_user_email"/>
            </column>
            <column name="image_url" type="varchar(256)"/>
            <column name="activated" type="boolean" valueBoolean="false">
                <constraints nullable="false" />
            </column>
            <column name="lang_key" type="varchar(10)"/>
            <%_ if (authenticationType !== 'oauth2') { _%>
            <column name="activation_key" type="varchar(20)"/>
            <column name="reset_key" type="varchar(20)"/>
            <%_ } _%>
            <column name="created_by" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="created_date" type="timestamp"/>
            <%_ if (authenticationType !== 'oauth2') { _%>
            <column name="reset_date" type="timestamp">
                <constraints nullable="true"/>
            </column>
            <%_ } _%>
            <column name="last_modified_by" type="varchar(50)"/>
            <column name="last_modified_date" type="timestamp"/>
        </createTable>

        <createTable tableName="<%= jhiTablePrefix %>_authority">
            <column name="name" type="varchar(50)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
        </createTable>

        <createTable tableName="<%= jhiTablePrefix %>_user_authority">
            <column name="user_id" type="<%_ if (authenticationType === 'oauth2') { _%>varchar(100)<%_ } else { _%>bigint<%_ } _%>">
                <constraints nullable="false"/>
            </column>
            <column name="authority_name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <addPrimaryKey columnNames="user_id, authority_name" tableName="<%= jhiTablePrefix %>_user_authority"/>
    <%_ if (authenticationType === 'session') { _%>

        <createTable tableName="<%= jhiTablePrefix %>_persistent_token">
            <column name="series" type="varchar(20)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="user_id" type="bigint"/>
            <column name="token_value" type="varchar(20)">
                <constraints nullable="false" />
            </column>
            <column name="token_date" type="date"/>
            <column name="ip_address" type="varchar(39)"/>
            <column name="user_agent" type="varchar(255)"/>
        </createTable>
    <%_ } _%>

        <addForeignKeyConstraint baseColumnNames="authority_name"
                                 baseTableName="<%= jhiTablePrefix %>_user_authority"
                                 constraintName="fk_authority_name"
                                 referencedColumnNames="name"
                                 referencedTableName="<%= jhiTablePrefix %>_authority"/>

        <addForeignKeyConstraint baseColumnNames="user_id"
                                 baseTableName="<%= jhiTablePrefix %>_user_authority"
                                 constraintName="fk_user_id"
                                 referencedColumnNames="id"
                                 referencedTableName="<%= jhiTablePrefix %>_user"/>

    <%_ if (authenticationType !== 'oauth2') { _%>
        <addNotNullConstraint   columnName="password_hash"
                                columnDataType="varchar(60)"
                                tableName="<%= jhiTablePrefix %>_user"/>
    <%_ } _%>
    <%_ if (authenticationType === 'session') { _%>

        <addForeignKeyConstraint baseColumnNames="user_id"
                                 baseTableName="<%= jhiTablePrefix %>_persistent_token"
                                 constraintName="fk_user_persistent_token"
                                 referencedColumnNames="id"
                                 referencedTableName="<%= jhiTablePrefix %>_user"/>
    <%_ } _%>
    <%_ if (authenticationType !== 'oauth2' || (authenticationType === 'oauth2' && applicationType !== 'microservice')) { _%>
        <<% if (prodDatabaseType === 'mssql') { %>ext:<% } %>loadData
                  file="config/liquibase/data/user.csv"
                  separator=";"
                  <%_ if (prodDatabaseType === 'mssql') { _%>
                  identityInsertEnabled="true"
                  <%_ } _%>
                  tableName="<%= jhiTablePrefix %>_user">
            <%_ if (prodDatabaseType === 'postgresql') { _%>
            <column name="id" type="numeric"/>
            <%_ } _%>
            <column name="activated" type="boolean"/>
            <column name="created_date" type="timestamp"/>
        </<% if (prodDatabaseType === 'mssql') { %>ext:<% } %>loadData>
        <dropDefaultValue tableName="<%= jhiTablePrefix %>_user" columnName="created_date" columnDataType="${datetimeType}"/>
        <<% if (prodDatabaseType === 'mssql') { %>ext:<% } %>loadData
                  file="config/liquibase/data/authority.csv"
                  separator=";"
                  <%_ if (prodDatabaseType === 'mssql') { _%>
                  identityInsertEnabled="true"
                  <%_ } _%>
                  tableName="<%= jhiTablePrefix %>_authority">
            <column name="name" type="string"/>
        </<% if (prodDatabaseType === 'mssql') { %>ext:<% } %>loadData>

        <<% if (prodDatabaseType === 'mssql') { %>ext:<% } %>loadData
                  file="config/liquibase/data/user_authority.csv"
                  separator=";"
                  <%_ if (prodDatabaseType === 'mssql') { _%>
                  identityInsertEnabled="true"
                  <%_ } _%>
                  tableName="<%= jhiTablePrefix %>_user_authority">
            <column name="user_id" type="numeric"/>
        </<% if (prodDatabaseType === 'mssql') { %>ext:<% } %>loadData>
    <%_ } _%>
<%_ } _%>
    </changeSet>

    <changeSet author="jhipster" id="00000000000002" context="test">
        <createTable tableName="<%= jhiTablePrefix %>_date_time_wrapper">
            <column <% if (isAutoIncrementDB) { _%> autoIncrement="${autoIncrement}" <%_ } %> name="id" type="BIGINT">
                <constraints primaryKey="true" primaryKeyName="<%= jhiTablePrefix %>_date_time_wrapperPK"/>
            </column>
            <column name="instant" type="timestamp"/>
            <column name="local_date_time" type="timestamp"/>
            <column name="offset_date_time" type="timestamp"/>
            <column name="zoned_date_time" type="timestamp"/>
            <column name="local_time" type="time"/>
            <column name="offset_time" type="time"/>
            <column name="local_date" type="date"/>
        </createTable>
    </changeSet>
</databaseChangeLog>
